let sqlCheck = {
    username: '',
    userkey: '',
    userId: '',
    role: 'N',
    coin: 0,
    exp: 0,
    edu_score: 0,
    item: [],
    wear: {},
    task: [],
    lastactive: 0,
    levelReset: false,
    jt: 0,
    jtNeed1: 0,
    kkNeed1: 0,
    bk: 0,
    miaoReward: 0,
    gqReward: 0,
    bc: 0,
}
Object.freeze(sqlCheck)//冻结集合无法改变
global.sqlCheck = sqlCheck
let Storage = storage.getGroupStorage('storage')
global.Storage = Storage;
global.getPlayerData = getPlayerData
async function getPlayerData(entity = new GameEntity) {
    var a
    while (true) {
        try {
            if (entity.player.userKey != undefined) {
                a = await Storage.get(entity.player.userKey)
            } else {
                a = await Storage.get(entity)

            }
            if (a != undefined) {
                if (a.value != undefined) {
                    return a.value
                }
            }
            return null
        } catch (e) {
            console.log(`${entity.player.name}加载失败，正在重试...`)
            await sleep(500);
        }
    }
}
global.checkKeys = checkKeys
function checkKeys(object1 = {}, object2 = {}) {//object1是被修改对象，object2是参照对象
    var object3 = new Object(object2)
    for (let i in object3) {
        if (object1[i] == undefined) {
            object1[i] = object3[i]
        } else if (typeof object1[i] == 'object') {
            checkKeys(object1[i], object3[i])
        }
    }
}
global.loadPlayer = loadPlayer
async function loadPlayer(entity = new GameEntity) {
    entity.save = false;
    var data = await getPlayerData(entity);
    if (data) {
        for (let i in data) {
            entity.data[i] = data[i]
        }
        for (let i in sqlCheck) {
            if (entity.data[i] == undefined) {
                entity.data[i] = JSON.parse(JSON.stringify(sqlCheck[i]))
            } else {
                if (typeof entity.data[i] == 'array' || typeof entity.data[i] == 'object') {
                    checkKeys(entity.data[i], sqlCheck[i])
                }
            }
        }
        entity.save = true;

        return '有存档'
    } else {
        world.say(`欢迎${entity.player.name}第一次加入游戏！`)
        entity.save = true;
        return '无存档'
    }
}
global.savePlayer = savePlayer
async function savePlayer(entity = new GameEntity) {//玩家存档
    if (entity.save == false) return;
    if (entity._emergency) return
    if (entity.isGuest()) return

    entity.lastSave = new Date().getTime();
    while (1) {
        try {
            var data = {}
            for (let i in sqlCheck) {
                data[i] = entity.data[i]
            }
            await Storage.update(entity.player.userKey, () => {

                return data
            })
            return { type: "success" };

        } catch (e) {
            console.log(`保存错误${e}`);
            await sleep(500);
            return { type: "error", data: e };
        }
    }
}
global.deleteAllData = deleteAllData
async function deleteAllData() {
    var sqlDataList = await Storage.list({
        cursor: 0
    });
    world.querySelectorAll('player').forEach(x => x.save = false);
    try {
        while (true) {
            for (let sqlData of sqlDataList.getCurrentPage()) {
                await Storage.remove(sqlData.key)
            }
            if (sqlDataList.isLastPage) break;
            await sqlDataList.nextPage();
        };
    } catch (e) { }
};


